﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MediaRecommender.Model;
using MediaRecommender.Security;

namespace MediaRecommender.Controller
{
    //Business logic for users and methods to add users the approriate collectiong class
    //Cant currently remove users
    public class UserRegistrationController
    {
        private UserCollection _userCollection = UserCollection.Instance;

        public UserRegistrationController()
        { }

        public void AddUserToDatabase(User toAdd)
        {
            if (!UserCollection.Instance.Add(toAdd))
                throw new ArgumentException("User already exist.");

        }

        public bool CheckIfUserIsValid(string name, string password, DateTime birth, Sex gender,  List<int> pref)
        {
            //Testing if the username is shorter than 15 chars and longer or equal to 3 chars
            if (name.Length < 15 && name.Length >= 3)
            {
                //Checking if password is longer than 5 chars and smaller or equal to 30
                if (password.Length > 5 && password.Length <= 30)
                {
                    User user = UserCollection.Instance.GetUser(name);
                    //Checking if the username alrdy exists in the database
                    if (user == null)
                    {
                        User toStore = new User(name, password, birth, gender, pref);
                        AddUserToDatabase(toStore);
                        return true;

                    }
                    else throw new ArgumentException("Username already exists");
                }
                else throw new ArgumentException("Password needs to be between 6 and 30 characters long");
            }
            else throw new ArgumentException("Username must be between 3 and 14 characters long");
        }

        public void WriteUserDbToFile()
        {
            _userCollection.WriteDbToFile();
        }

        public void ReadUserDbFromFile()
        {
            _userCollection.ReadDbFromFile();
        }
    }
}